package com.dmcloud.order.config;

import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by xiaodao
 * date: 2020/3/30
 */
@Configuration
public class PrometheusMetricsConfig {

    @Autowired
    private PrometheusMeterRegistry prometheusMeterRegistry;

    /**
     * 只增不减的
     * @return
     */
    @Bean
    public Counter requestCounter(){
        return  Counter.build("is_request_count","count_request_by_service")

                .labelNames("service","method","code").
                        register(prometheusMeterRegistry.getPrometheusRegistry());
    }


    @Bean
    public Summary requestLatency(){
        return Summary.build("is_request_Latency","monitor request latency by service")
                //0.5 10个请求 10ms 50%的请求可以在 50ms返回 后面的指标是误差
                .quantile(0.5,0.05)
                //0.9 10个请求  10ms  20ms 30ms  90ms  90%的请求可以在 90ms返回 后面的指标是误差
                .quantile(0.9,0.01)

                .labelNames("service","method","code").register(prometheusMeterRegistry.getPrometheusRegistry());
    }
}
